package cn.yunhe.test;

import cn.yunhe.dao.AdminMapper;
import cn.yunhe.dao.NewsMapper;
import cn.yunhe.entity.Admin;
import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import cn.yunhe.service.NewsService;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class Demo1 {
    SqlSession session = null;
    AdminMapper adminMapper = null;
    NewsMapper newsMapper = null;

    @Before
    public void init() {
        session = SqlSessionFactoryUtil.getSqlSession();
        adminMapper = session.getMapper(AdminMapper.class);
        newsMapper = session.getMapper(NewsMapper.class);
    }

    @Test
    public void addTest() {
        Admin admin = new Admin("zhangsan", "123");
        System.out.println(adminMapper.registerAdmin(admin));
    }

    @Test
    public void loginTest() {
        Admin admin = new Admin("zhangsan", "123");
        Admin a = adminMapper.loginAdmin(admin);

        System.out.println(a.getAdminName());

    }


    @Test
    public void newsListTest() {
        /*Page page = new Page();
        page.setPageStart(0);
        page.setPageSize(3);
        List<News> newsList = newsMapper.getNewsByPage(page);*/

        NewsService newsService = new NewsService();
        List<News> newsList = newsService.getNewsByPage(2, "");


        for (News n : newsList) {
            System.out.println(n.getTitle() + "," + n.getBlock().getBlockName());
        }
    }

    public void keywordTest() {

    }


    @Test
    public void newsCountTest() {
        NewsService newsService = new NewsService();
        System.out.println(newsService.getNewsCount());
    }


    @Test
    public void newsKeywordTest() {
        NewsService newsService = new NewsService();
        List<News> newsList = newsService.getNewsByPage(2, null);


        for (News n : newsList) {
            System.out.println(n.getTitle() + "," + n.getBlock().getBlockName());
        }
    }

    @Test
    public void threeJoinTest() {
        NewsService newsService = new NewsService();
        News news = newsService.getNewsByNewsid(7);

        System.out.println(news.getTitle() + "," + news.getBlock().getBlockName() + "," + news.getAdmin().getAdminName());

    }


    @After
    public void destory() {
        session.commit();
        session.close();
    }

}
